var ctx=window.daijia.ctx; 
var latitude,longitude;
$(document).ready(function(){
	locationMe();
});
function locationMe(){
	if(navigator && navigator.geolocation){
		navigator.geolocation.getCurrentPosition(function (position) {
		    var coords = position.coords;
			latitude=coords.latitude;
			longitude=coords.longitude;
			loadDriver();
			$("img").hide();
		},function(error){
			
		    switch(error.code) {
		        case error.TIMEOUT:
		        	$("#fail").replaceWith('<p style="text-align: center; font-size:16px;color:#8b8b8b;">找不到你所在的位置,你可以点击<a href="'+ctx+'/wx/v1/memberOrder">一键下单</a></p>');
		          	break;
		        case error.POSITION_UNAVAILABLE:
		        	$("#fail").replaceWith('<p style="text-align: center; font-size:16px;color:#8b8b8b;">找不到你所在的位置,你可以点击<a href="'+ctx+'/wx/v1/memberOrder">一键下单</a></p>');
		            break;
		        case error.PERMISSION_DENIED:
		        	$("#fail").replaceWith('<p style="text-align: center; font-size:16px;color:#8b8b8b;">找不到你所在的位置,你可以点击<a href="'+ctx+'/wx/v1/memberOrder">一键下单</a></p>');
		            break;
		        case error.UNKNOWN_ERROR:
		        	$("#fail").replaceWith('<p style="text-align: center; font-size:16px;color:#8b8b8b;">找不到你所在的位置,你可以点击<a href="'+ctx+'/wx/v1/memberOrder">一键下单</a></p>');
		            break;
		    }
		},{enableHighAccuracy:true,timeout:3000,maximumAge:4000});
	}else{
		$.scojs_message("您的浏览器不能定位，请更换浏览器。", $.scojs_message.TYPE_ERROR);
	}
}
function xiadan(driverId){
	window.location.href =ctx+"/m/driverDetails?driverId="+driverId+"&longitude="+longitude+"&latitude="+latitude;
}
function loadDriver(){
		$.ajax({
	        type: "POST",
	        async:true,
	        url: ctx+"/m/queryNearDrivers",
	        data: {longitude:longitude, latitude:latitude},	//传到后台的经纬度
	        dataType: "json",
	        success: function(instance){ 
	        	var driverList=instance.data;
	        	if(driverList.length!=0){
	        		var content="";
	        		 $.each(driverList, function (i,d) {  
	        			 var imgUrl=ctx+'/assets/img/a.png';
	        			 if(d.driverHead!=null){
	        				 imgUrl=ctx+'/upload/driver/'+d.driverHead;
	        			 }
	        			 content+='<div class="container-fluid" style="margin-top:1px; padding-right: 0px;" onclick="xiadan('+d.driverId+');">';
	        				 content+='<div class="row">';
	        					 content+='<div class="col-xs-12">';
	        						 
	        						 content+='<div class="col-xs-2">';
	        							 content+='<img src="'+imgUrl+'"class="img-circle"/>';  
	        						 content+='</div>';
	        							 
	        						 content+='<div class="col-xs-9">';
	        							 content+='<div class="col-xs-12" id="worp-2">';
	        								content+='<h4>'+d.driverName+'</h4>';
	        								content+='<div class="col-xs-4">';
	        									content+='代驾:'+d.driverTimes+'次 ';
	        								content+='</div>'	
	        								content+='<div class="col-xs-4">';
	        									content+='驾龄:'+d.driverJialing+'年';
	        								content+='</div>';
	        								content+='<div class="col-xs-4">';
	        									content+='距离:'+d.driverDistance;
	    	        						content+='</div>';	
	    	        					 content+='</div>';
	    	        					 content+='<div class="col-xs-12" style="padding-left: 0px; width: 85px; height: 16px; line-height:0;">';	 
	    	        						 content+='<div class="col-xs-12" style="width:'+d.driverStar*120/5+'%;height: 16px;padding-left: 0px;padding-right: 0px;background-color: red; line-height:0; margin-bottom: 5px;" >';
	    	        							 content+='<img src="'+ctx+'/assets/img/5xing.png" />';
	    	        						 content+='</div>';
	    	        					 content+='</div>';	 
	        						 content+='</div>';
	        							 
	        						 content+='<div class="col-xs-1">';
	        							 content+='<img src="'+ctx+'/assets/img/JI.png" />';
	        						 content+='</div>';
	        							 
	        					 content+='</div>';	 
	        				 content+='</div>';
        				 content+='</div>';
	        		 })
	        			 $("body").empty();
	        		     $("body").append(content);
	        	}else{
	        		
	        		$("#fail").replaceWith('<p style="text-align: center; font-size:16px;color:#8b8b8b;">抱歉附近没有服务人员,你可以点击<a href="'+ctx+'/wx/v1/memberOrder">一键下单</a></p>');
	        	}
	        }
	    });
}
